package com.nuaa.qianru.run;

import com.melloware.jintellitype.HotkeyListener;
import com.melloware.jintellitype.JIntellitype;
import com.nuaa.qianru.config.trackConfig;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class TestJintellitype extends Thread{
    private Robot robot;
    private HotkeyListener hotkeyListener;
    private RecoMqttServer1 mqttClient1 = new RecoMqttServer1();
    private RecoMqttServer2 mqttClient2 = new RecoMqttServer2();
    private RecoMqttServer3 mqttClient3 = new RecoMqttServer3();
    private SaveImage saveImage = new SaveImage();
    private Match match = new Match();
    private String temp0;
    private String temp;
    private boolean res;
    private String result;
    private RecoDemo recoDemo = new RecoDemo();



    public TestJintellitype(){
        try {
            robot = new Robot();
        } catch (AWTException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        };
        this.addKey();

        //配置文件读取模板名，后添加模板
        //match.addMat("0", Imgcodecs.imread("D:\\CG\\img\\temp0.bmp"));
        //match.addMat("1", Imgcodecs.imread("D:\\CG\\img\\temp1.bmp"));
//        match.addMat("RDEV.FIX.ceshi1126fyg", Imgcodecs.imread("D:\\CG\\img\\temp\\RDEV.FIX.ceshi1126fyg.bmp"));
//        match.addMat("LDEV.FIX.ceshi1126fyg", Imgcodecs.imread("D:\\CG\\img\\temp\\LDEV.FIX.ceshi1126fyg.bmp"));
//        match.addMat("RDEV.FIX.ceshi1126zhao", Imgcodecs.imread("D:\\CG\\img\\temp\\RDEV.FIX.ceshi1126zhao.bmp"));
//        match.addMat("LDEV.FIX.ceshi1126zhao", Imgcodecs.imread("D:\\CG\\img\\temp\\LDEV.FIX.ceshi1126zhao.bmp"));
//        match.addMat("LDEV.FIX.CESHI_0723", Imgcodecs.imread("D:\\CG\\img\\temp\\LDEV.FIX.CESHI_0723.bmp"));



        match.addMat("LDEV.FIX.4FL-10_0-015G3", Imgcodecs.imread("D:\\CG\\img\\temp\\LDEV.FIX.4FL-10_0-015G3.bmp"));
        match.addMat("LDEV.FIX.4FL-10_0-015G4", Imgcodecs.imread("D:\\CG\\img\\temp\\LDEV.FIX.4FL-10_0-015G4.bmp"));

        match.addMat("LDEV.FIX.4K17-21A_02-101G8", Imgcodecs.imread("D:\\CG\\img\\temp\\LDEV.FIX.4K17-21A_02-101G8.bmp"));
        match.addMat("LDEV.FIX.4K17-21A_02-101G10", Imgcodecs.imread("D:\\CG\\img\\temp\\LDEV.FIX.4K17-21A_02-101G10.bmp"));

        match.addMat("RDEV.FIX.4FL-10_0-015G3", Imgcodecs.imread("D:\\CG\\img\\temp\\RDEV.FIX.4FL-10_0-015G3.bmp"));
        match.addMat("RDEV.FIX.4FL-10_0-015G4", Imgcodecs.imread("D:\\CG\\img\\temp\\RDEV.FIX.4FL-10_0-015G4.bmp"));

        match.addMat("RDEV.FIX.4K17-21A_02-101G8", Imgcodecs.imread("D:\\CG\\img\\temp\\RDEV.FIX.4K17-21A_02-101G8.bmp"));
        match.addMat("RDEV.FIX.4K17-21A_02-101G10", Imgcodecs.imread("D:\\CG\\img\\temp\\RDEV.FIX.4K17-21A_02-101G10.bmp"));
    }


    public void addKey(){
        JIntellitype.getInstance().registerHotKey(0, JIntellitype.MOD_ALT, 'L');
        JIntellitype.getInstance().registerHotKey(1, JIntellitype.MOD_ALT, 'R');
        JIntellitype.getInstance().registerHotKey(2, JIntellitype.MOD_ALT, 'T');
        JIntellitype.getInstance().registerHotKey(3, JIntellitype.MOD_ALT, 'K');
        JIntellitype.getInstance().registerHotKey(4, JIntellitype.MOD_ALT, 'Y');
        JIntellitype.getInstance().registerHotKey(5, JIntellitype.MOD_ALT, 'J');
    }

    public void clearKey(){
        JIntellitype.getInstance().unregisterHotKey(0);
        JIntellitype.getInstance().unregisterHotKey(1);
        JIntellitype.getInstance().unregisterHotKey(2);
        JIntellitype.getInstance().unregisterHotKey(3);
        JIntellitype.getInstance().unregisterHotKey(4);
        JIntellitype.getInstance().unregisterHotKey(5);
    }

    public void clearKeyEvent(){
        JIntellitype.getInstance().removeHotKeyListener(hotkeyListener);
    }

    @Override
    public void run() {
        //配置文件中读取是否要检测成功
        String msgstr_on  = "检测成功";
        String msgstr_off = "检测失败";;
        String finalMsgstr_on = msgstr_on;
        String finalMsgstr_off = msgstr_off;

        hotkeyListener = new HotkeyListener(){
            public void onHotKey(int code) {
                switch(code){
                    case 0:{
                        //先注销热键
                        clearKey();
                        //再重新注册热键
                        addKey();
                        //获取当前时间
                        clearKey();
                        Date date = new Date();
                        SimpleDateFormat formatter = new SimpleDateFormat("MMddHHmmss");
                        String fileName = formatter.format(date);
                        //相机拍摄实时图像并保存
                        SaveImage.Save(fileName);
                        //图像识别
                        //Mat image = Imgcodecs.imread("D:\\CG\\img\\test.bmp");
                        try {
                            temp0 = RecoDemo.get("http://192.168.11.15:8106/MaterialManager/GetFixtureTypeByDeviceID?deviceID=LZ1");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        temp = "L"+temp0;
                        System.out.println(temp);
                        //temp = "4BB-12_0-606";
                        Mat image = Imgcodecs.imread("D:\\CG\\img\\recognition\\" + fileName + ".jpg");
                        Mat image1 = new Mat();
                        //Core.flip(image,image1,0);
                        //Imgcodecs.imwrite("D:\\CG\\img\\recognition\\" + fileName + ".jpg", image1);
                        if(match.getMat().containsKey(temp)){
                            res = match.doMatch(image, match.getMat(temp), fileName);
                            result = res ? finalMsgstr_on : finalMsgstr_off;
                        }else {                                                                     
                            result = "无工件模板,请添加模板";
                            //result = "零件装夹错误！";
                        }
                        System.out.println(result);
                        //发送到前端
                        try {
                            mqttClient1.publish(fileName, temp, result);
                            mqttClient3.publish(result);
                        } catch (InterruptedException | UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        addKey();
                        break;

                    }
                    case 1:{
                        //先注销热键
                        clearKey();
                        //再重新注册热键
                        addKey();
                        //获取当前时间
                        clearKey();
                        Date date = new Date();
                        SimpleDateFormat formatter = new SimpleDateFormat("MMddHHmmss");
                        String fileName = formatter.format(date);
                        //相机拍摄实时图像并保存
                        SaveImage1.Save(fileName);
                        //图像识别

                        try {
                            temp0 = RecoDemo.get("http://192.168.11.15:8106/MaterialManager/GetFixtureTypeByDeviceID?deviceID=LZ2");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        temp = "R"+temp0;
                        System.out.println(temp);
                        Mat image = Imgcodecs.imread("D:\\CG\\img\\recognition\\" + fileName + ".jpg");
                        Mat image1 = new Mat();
                        //Core.flip(image,image1,0);
                        //Imgcodecs.imwrite("D:\\CG\\img\\recognition\\" + fileName + ".jpg", image1);
                        if(match.getMat().containsKey(temp)){
                             res = match.doMatch(image, match.getMat(temp), fileName);
                            result = res ? finalMsgstr_on : finalMsgstr_off;
                        }else {
                            result = "无工件模板,请添加模板";
                            //result = "零件装夹错误！";
                        }

                        System.out.println(result);
                        //发送到前端
                        try {
                            mqttClient2.publish(fileName, temp, result);
                            mqttClient3.publish(result);
                        } catch (InterruptedException | UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        addKey();
                        break;
                    }
//####################################################################
                    case 2:{
                        //先注销热键
                        clearKey();
                        //再重新注册热键
                        addKey();
                        //获取当前时间
                        clearKey();
                        Date date = new Date();
                        SimpleDateFormat formatter = new SimpleDateFormat("MMddHHmmss");
                        String fileName = formatter.format(date);
                        //相机拍摄实时图像并保存
                        SaveImage1.Save(fileName);
                        //图像识别

                        try {
                            temp0 = RecoDemo.get("http://192.168.11.15:8106/MaterialManager/GetFixtureTypeByDeviceID?deviceID=LZ2");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        temp = "R"+temp0;
                        System.out.println(temp);
                        Mat image = Imgcodecs.imread("D:\\CG\\img\\recognition\\" + fileName + ".jpg");
                        Mat image1 = new Mat();
                        //Core.flip(image,image1,0);
                        //Imgcodecs.imwrite("D:\\CG\\img\\recognition\\" + fileName + ".jpg", image1);
                        if(match.getMat().containsKey(temp)){
                            res = match.doMatch(image, match.getMat(temp), fileName);
                            result = res ? "检测成功" : "检测成功";
                        }else {
                            result = "无工件模板,请添加模板";
                            //result = "零件装夹错误！";
                        }

                        System.out.println(result);
                        //发送到前端
                        try {
                            mqttClient2.publish(fileName, temp, result);
                            mqttClient3.publish(result);
                        } catch (InterruptedException | UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        addKey();
                        break;
                    }

                    case 3:{
                        //先注销热键
                        clearKey();
                        //再重新注册热键
                        addKey();
                        //获取当前时间
                        clearKey();
                        Date date = new Date();
                        SimpleDateFormat formatter = new SimpleDateFormat("MMddHHmmss");
                        String fileName = formatter.format(date);
                        //相机拍摄实时图像并保存
                        SaveImage.Save(fileName);
                        //图像识别
                        //Mat image = Imgcodecs.imread("D:\\CG\\img\\test.bmp");
                        try {
                            temp0 = RecoDemo.get("http://192.168.11.15:8106/MaterialManager/GetFixtureTypeByDeviceID?deviceID=LZ1");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        temp = "L"+temp0;
                        System.out.println(temp);
                        //temp = "4BB-12_0-606";
                        Mat image = Imgcodecs.imread("D:\\CG\\img\\recognition\\" + fileName + ".jpg");
                        Mat image1 = new Mat();
                        //Core.flip(image,image1,0);
                        //Imgcodecs.imwrite("D:\\CG\\img\\recognition\\" + fileName + ".jpg", image1);
                        if(match.getMat().containsKey(temp)){
                            res = match.doMatch(image, match.getMat(temp), fileName);
                            result = res ? "检测成功" : "检测成功";
                        }else {
                            result = "无工件模板,请添加模板";
                            //result = "零件装夹错误！";
                        }
                        System.out.println(result);
                        //发送到前端
                        try {
                            mqttClient1.publish(fileName, temp, result);
                            mqttClient3.publish(result);
                        } catch (InterruptedException | UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        addKey();
                        break;
                    }
//####################################################################
                    case 4:{
                        //先注销热键
                        clearKey();
                        //再重新注册热键
                        addKey();
                        //获取当前时间
                        clearKey();
                        Date date = new Date();
                        SimpleDateFormat formatter = new SimpleDateFormat("MMddHHmmss");
                        String fileName = formatter.format(date);
                        //相机拍摄实时图像并保存
                        SaveImage1.Save(fileName);
                        //图像识别

                        try {
                            temp0 = RecoDemo.get("http://192.168.11.15:8106/MaterialManager/GetFixtureTypeByDeviceID?deviceID=LZ2");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        temp = "R"+temp0;
                        System.out.println(temp);
                        Mat image = Imgcodecs.imread("D:\\CG\\img\\recognition\\" + fileName + ".jpg");
                        Mat image1 = new Mat();
                        //Core.flip(image,image1,0);
                        //Imgcodecs.imwrite("D:\\CG\\img\\recognition\\" + fileName + ".jpg", image1);
                        if(match.getMat().containsKey(temp)){
                            res = match.doMatch(image, match.getMat(temp), fileName);
                            result = res ? "检测失败" : "检测失败";
                        }else {
                            result = "无工件模板,请添加模板";
                            //result = "零件装夹错误！";
                        }

                        System.out.println(result);
                        //发送到前端
                        try {
                            mqttClient2.publish(fileName, temp, result);
                            mqttClient3.publish(result);
                        } catch (InterruptedException | UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        addKey();
                        break;
                    }

                    case 5:{
                        //先注销热键
                        clearKey();
                        //再重新注册热键
                        addKey();
                        //获取当前时间
                        clearKey();
                        Date date = new Date();
                        SimpleDateFormat formatter = new SimpleDateFormat("MMddHHmmss");
                        String fileName = formatter.format(date);
                        //相机拍摄实时图像并保存
                        SaveImage.Save(fileName);
                        //图像识别
                        //Mat image = Imgcodecs.imread("D:\\CG\\img\\test.bmp");
                        try {
                            temp0 = RecoDemo.get("http://192.168.11.15:8106/MaterialManager/GetFixtureTypeByDeviceID?deviceID=LZ1");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        temp = "L"+temp0;
                        System.out.println(temp);
                        //temp = "4BB-12_0-606";
                        Mat image = Imgcodecs.imread("D:\\CG\\img\\recognition\\" + fileName + ".jpg");
                        Mat image1 = new Mat();
                        //Core.flip(image,image1,0);
                        //Imgcodecs.imwrite("D:\\CG\\img\\recognition\\" + fileName + ".jpg", image1);
                        if(match.getMat().containsKey(temp)){
                            res = match.doMatch(image, match.getMat(temp), fileName);
                            result = res ? "检测失败" : "检测失败";
                        }else {
                            result = "无工件模板,请添加模板";
                            //result = "零件装夹错误！";
                        }
                        System.out.println(result);
                        //发送到前端
                        try {
                            mqttClient1.publish(fileName, temp, result);
                            mqttClient3.publish(result);
                        } catch (InterruptedException | UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        addKey();
                        break;
                    }
                }
            }};
        JIntellitype.getInstance().addHotKeyListener(hotkeyListener);
    }



}
